package com.kaikeba.estack.common;

import com.fasterxml.jackson.annotation.JsonCreator;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Optional;

/**
 * @Auth xingwan
 * @Date 2020/10/5 1:20
 **/
public enum OrderEnum {

    ASC, DESC;

    @JsonCreator
    public static OrderEnum toOrderEnum(String order) {
        //默认以升序排列
        if (StringUtils.isBlank(order)) {
            return ASC;
        }

        Optional<OrderEnum> any = Arrays.stream(values())
                .filter(key -> key.name()
                        .toLowerCase()
                        .equals(order.toLowerCase()))
                .findAny();
        return any.orElse(ASC);

    }

    @Override
    public String toString() {
        return this.name()
                .toLowerCase();
    }
}
